package com.keydom.ropeskipping.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class Circleprogressbar extends View {
    private int progress=0;
    private int maxprogress;
    //绘图
    private Paint Pathpaint;
    private Paint fillpaint;//渐变
    //绘制区域
    private RectF oval;
    //颜色
    private int[] arcColors={0xFFFF9800,0xFFF5F2F2,0xFFFF9800};
    //背景灰，边框灰
    private  int pathColor=0xFF255DE4;
    private  int borderColor=0xFF255DE4;
    private int pathwidth=10;
    //宽高
    private int w;
    private int h;
    //半径
    private int radius=120;
    //梯度渲染
    private SweepGradient sweepGradient;
    //重置
    private boolean reset=false;

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        this.invalidate();
    }

    public int getMaxprogress() {
        return maxprogress;
    }

    public void setMaxprogress(int maxprogress) {
        this.maxprogress = maxprogress;
        this.invalidate();
    }

    public int[] getArcColors() {
        return arcColors;
    }

    public void setArcColors(int[] arcColors) {
        this.arcColors = arcColors;
    }

    public int getPathColor() {
        return pathColor;
    }

    public void setPathColor(int pathColor) {
        this.pathColor = pathColor;
    }

    public int getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public boolean isReset() {
        return reset;
    }

    public void setReset(boolean reset) {
        this.reset = reset;
        if (reset) {
            this.progress = 0;
            this.invalidate();
        }
    }

    public Circleprogressbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        Pathpaint = new Paint();
        // 设置是否抗锯齿
        Pathpaint.setAntiAlias(true);
        // 帮助消除锯齿
        Pathpaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        // 设置中空的样式
        Pathpaint.setStyle(Paint.Style.STROKE);
        Pathpaint.setDither(true);
        Pathpaint.setStrokeJoin(Paint.Join.ROUND);

        fillpaint = new Paint();
        // 设置是否抗锯齿
        fillpaint.setAntiAlias(true);
        // 帮助消除锯齿
        fillpaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        // 设置中空的样式
        fillpaint.setStyle(Paint.Style.STROKE);
        fillpaint.setDither(true);
        fillpaint.setStrokeJoin(Paint.Join.ROUND);
        this.oval = new RectF();
        //以上初始化绘制
//        this.emboss = new EmbossMaskFilter(this.direction, this.light, this.specular, this.blur);
//        this.mBlur = new BlurMaskFilter(20.0F, BlurMaskFilter.Blur.NORMAL);
//        sweepGradient = new SweepGradient((float) (this.width / 2), (float) (this.height / 2), this.arcColors, (float[]) null);
    }
    //重置绘制方法
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.reset) {
            canvas.drawColor(0xFFFF9800);
            this.reset = false;
        }

        this.w = this.getMeasuredWidth();
        this.h = this.getMeasuredHeight();
        this.radius = this.getMeasuredWidth() / 2 - this.pathwidth;
        //画笔颜色
        this.Pathpaint.setColor(this.pathColor);
        //设置画笔宽度
        this.Pathpaint.setStrokeWidth((float) this.pathwidth);
        //添加浮雕效果
//        this.Pathpaint.setMaskFilter(this.emboss);
        // 在中心的地方画个半径为r的圆
        canvas.drawCircle((float) (this.w / 2), (float) (this.h / 2), (float) this.radius, this.Pathpaint);
        this.Pathpaint.setStrokeWidth(0.5F);
        this.Pathpaint.setColor(this.borderColor);
        canvas.drawCircle((float) (this.w / 2), (float) (this.h / 2), (float) (this.radius + this.pathwidth / 2) + 0.5F, this.Pathpaint);
        canvas.drawCircle((float) (this.w / 2), (float) (this.h / 2), (float) (this.radius - this.pathwidth / 2) - 0.5F, this.Pathpaint);
//  渐变
        SweepGradient sweepGradient = new SweepGradient((float)(this.w / 2), (float)(this.h / 2), this.arcColors, (float[])null);
        //设置着色器
        this.fillpaint.setShader(sweepGradient);
//        this.fillpaint.setMaskFilter(this.mBlur);
        this.fillpaint.setStrokeCap(Paint.Cap.ROUND);
        this.fillpaint.setStrokeWidth((float) this.pathwidth);
        this.oval.set((float) (this.w / 2 - this.radius), (float) (this.h / 2 - this.radius), (float) (this.w / 2 + this.radius), (float) (this.h / 2 + this.radius));
        canvas.drawArc(this.oval, -90.0F, (float) this.progress / (float) this.maxprogress * 360.0F, false, this.fillpaint);
    }

    //重写长宽
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height= MeasureSpec.getSize(heightMeasureSpec);
        int width= MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width,height);
    }
}
